//
//  CTMacro.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit

// MARK: - ********* Device Size
public var kScreenWid: CGFloat { return UIScreen.main.bounds.size.width }

public var kScreenHei: CGFloat { return UIScreen.main.bounds.size.height }

public var kIsX: Bool { return ScreenSize.shared.isIphonex }

public var kNavBtm: CGFloat { return kNavTop + 44 }

public var kNavTop: CGFloat {
    if kIsX { return 44 }
    return 20
}

public var kHairHei: CGFloat {
    if kIsX { return 30 }
    return 0
}

public var kSafeBtm: CGFloat {
    if kIsX { return 34 }
    return 0
}

public var kTabbarHei: CGFloat {
    if kIsX { return 34 + 49 }
    return 49
}

// MARK: - ********* Public Quick Funcs
///  printf
public func k_log<T>(_ item: T,
                        file: String = #file,
                        line: Int = #line,
                        function: String = #function) {
    #if DEBUG
    print("================== debug log ==================")
    print("time: \(currentTime)")
    print("file: \((file as NSString).lastPathComponent) \t line: \(line) \t func: \(function)")
    print("log:  \(item)\n")
    #endif
}

fileprivate var currentTime: String {
    let timeStr = "\(Date().addingTimeInterval(8 * 60 * 60))"
    return String(timeStr.prefix(20))
}

public func k_logViewControllerDeinit(_ vc: UIViewController) {
    #if DEBUG
    guard let viewDesc = "\(vc.self)".split(separator: ".").last
    else { return }
    print("Deinit:  --->  ViewController:\t\(String(viewDesc))")
    #endif
}

public func k_logViewDeinit(_ view: UIView) {
    #if DEBUG
    guard let desc = "\(view.self)".split(separator: ";").first,
         let viewDesc = String(desc).split(separator: ".").last
        else { return }
    print("Deinit:  --->  View:\t\t\t\(String(viewDesc))")
    #endif
}

public func k_logViewModelDeinit(_ vm: AnyObject) {
    #if DEBUG
    guard let vmDesc = "\(vm.self)".split(separator: ".").last
    else { return }
    print("Deinit:  --->  ViewModel:\t\t\(String(vmDesc))")
    #endif
}

///  主线程 延时
public func kDelay(_ delay: Double, onQueue queue: DispatchQueue = DispatchQueue.main,  closure: @escaping () -> Void) {
    queue.asyncAfter(deadline: .now() + delay, execute: closure)
}

public extension DispatchQueue {
    // This method will dispatch the `block` to self.
    // If `self` is the main queue, and current thread is main thread, the block
    // will be invoked immediately instead of being dispatched.
    func safeAsync(_ block: @escaping () -> Void) {
        if self === DispatchQueue.main && Thread.isMainThread {
            block()
        } else {
            async { block() }
        }
    }
}

// MARK: - ********* app信息获取
public final class AppInfo {
    /// 当前应用名称
    static var appDisplayName: String {
        return (Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String) ?? ""
    }
    /// 当前应用bundle
    static var bundleId: String {
        return (Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String) ?? ""
    }
    /// 当前应用软件版本  比如：2.0.2
    static var appCurVersion: String {
        return (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? ""
    }
    /// 手机别名： 用户定义的名称
    static var userPhoneName: String {
        return UIDevice.current.name
    }
    /// 设备名称 e.g. "iOS"
    static var deviceName: String {
        return UIDevice.current.systemName
    }
    /// 手机系统版本 如： 12.0
    static var systemVersion: String {
        return UIDevice.current.systemVersion
    }
}

// MARK:- 快速创建block
typealias VoidClosure = (() -> Void)

